<template>
  <el-card shadow="hover" class="m-20">
    <el-form label-width="120px" :model="formData">
      <el-form-item label="SPU名称"
        ><el-input v-model="formData.spuName" placeholder="SPU名称"></el-input
      ></el-form-item>
      <el-form-item label="品牌">
        <el-select v-model="formData.tmId">
          <el-option
            v-for="item in trademarkList"
            :label="item.tmName"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="SPU描述">
        <el-input
          type="textarea"
          placeholder="SPU描述"
          v-model="formData.description"
        ></el-input>
      </el-form-item>
      <el-form-item label="SPU图片">
        <el-upload
          :action="`${BASE_URL}/admin/product/fileUpload`"
          list-type="picture-card"
          :auto-upload="false"
          :on-change="handleChange"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            />
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
              >
                <i class="el-icon-download"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>
        <el-dialog>
          <img width="100%" src="" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-select v-model="selectBaseSaleAttr">
          <el-option
            v-for="item in baseSaleAttrList"
            :key="item.id"
            :value="`${item.name}:${item.id}`"
            :label="item.name"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-plus"
          class="ml-10"
          @click="addSpuSaleAttr"
        >
          添加销售属性
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-table border :data="this.formData.spuSaleAttrList">
          <el-table-column
            label="序号"
            width="80px"
            type="index"
          ></el-table-column>
          <el-table-column label="属性名" prop="saleAttrName"></el-table-column>
          <el-table-column label="属性值列表">
            <el-tag
              :key="tag"
              v-for="tag in formData.spuSaleAttrList.spuSaleAttrValueList"
              closable
              :disable-transitions="false"
              @close="handleClose(tag)"
            >
              {{ tag }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
            >
            </el-input>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput"
              >+</el-button
            >
          </el-table-column>
          <el-table-column label="属性名" prop="saleAttrName">
            <template v-slot="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="small"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { getSpuTrademarkList, getSpuBaseSaleAttrList } from "@/api/addSpu";
export default {
  name: "AddSpu",
  data() {
    const BASE_URL = process.env.VUE_APP_BASE_API;
    return {
      BASE_URL,
      formData: {
        category3Id: 0,
        description: "",
        id: undefined,
        spuImageList: [
          {
            id: undefined,
            imgName: "",
            imgUrl: "",
            spuId: undefined,
          },
        ],
        spuName: "",
        spuSaleAttrList: [],
        tmId: undefined,
      },
      trademarkList: [],
      baseSaleAttrList: [],
      selectBaseSaleAttr: "",
      selectBaseSaleAttrList: [],
      inputVisible: false,
      inputValue: "",
    };
  },
  async mounted() {
    const res = await getSpuTrademarkList();
    const res2 = await getSpuBaseSaleAttrList();
    // console.log(res);
    // console.log(res2);
    this.trademarkList = res.data;
    this.baseSaleAttrList = res2.data;
  },
  methods: {
    // 图片检验
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 < 200;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 500kb!");
      }
      return isJPG && isLt2M;
    },
    // 上传成功
    handleAvatarSuccess(file) {
      this.formData.logoUrl = file.data;
    },
    // 图片状态改变
    handleChange(file, fileList) {},
    // spu销售属性的变化
    addSpuSaleAttr() {
      if (this.selectBaseSaleAttr === "") {
        return;
      }
      const [name, id] = this.selectBaseSaleAttr.split(":");
      this.formData.spuSaleAttrList.push({
        baseSaleAttrId: +id,
        saleAttrName: name,
        spuSaleAttrValueList: [],
      });
      this.baseSaleAttrList = this.baseSaleAttrList.filter((baseSaleAttr) => {
        if (
          this.formData.spuSaleAttrList.some((spuSaleAttr) => {
            return spuSaleAttr.baseSaleAttrId === baseSaleAttr.id;
          })
        ) {
          return false;
        }
        return true;
      });
      this.selectBaseSaleAttr = "";
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      // this.$nextTick((_) => {
      //   this.$refs.saveTagInput.$refs.input.focus();
      // });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
  },
};
</script>

<style scoped>
.tmImg {
  width: 80px;
  height: 50px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #000;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #c6c4c4;
}
.avatar-uploader-icon:hover {
  border-color: #409eff;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
